<template>
	<el-dialog
		:title="title + '报社'"
		:visible.sync="addComShow"
		width="35%"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		:before-close="cancel"
	>
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="90px" class="demo-ruleForm" label-position="left">
			<el-form-item label="报社名称" prop="newspaper">
				<el-input v-model.trim="form.newspaper"></el-input>
			</el-form-item>
			<el-form-item label="报社邮箱" prop="newspaperEmail">
				<el-popover placement="top-start" trigger="focus" content="如果有多个邮箱，请使用分号 “  ; ” 分隔">
					<el-input slot="reference" v-model.trim="form.newspaperEmail" @input="emailChange(form, 'newspaperEmail')"></el-input>
				</el-popover>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="cancel" size="small">取 消</el-button>
			<el-button type="primary" @click="addConfirm" size="small">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { addNewspaper } from '@/api/productApi/newspaper'
export default {
	props: {
		title: {
			type: String,
			default: ''
		},
		addComShow: {
			type: Boolean,
			default: false
		},
		comIsShow: {
			type: Function,
			default: Function
		},
		queryFundManagerList: {
			type: Function,
			default: Function
		}
	},
	data() {
		return {
			form: {},
			rules: {
				newspaper: [{ required: true, message: '请输入报社名称', trigger: 'blur' }],
				newspaperEmail: [{ required: true, message: '请输入报社邮箱', trigger: 'blur' }]
			}
		}
	},
	mounted() {},
	methods: {
		cancel() {
			this.$emit('comIsShow')
			this.form = {}
			this.$refs.ruleForm.resetFields()
		},

		// 新增或编辑
		addConfirm() {
			this.$refs.ruleForm.validate(valid => {
				if (valid) {
					const form = {
						newspaperEmail: this.form.newspaperEmail,
						newspaper: this.form.newspaper
					}
					if (this.title === '编辑') {
						form.newspaperId = this.form.newspaperId
					}
					addNewspaper(form).then(res => {
						if (res.msgCode === 'Y') {
							this.$message({ type: 'success', message: res.msgDesc })
							this.$emit('queryFundManagerList')
							this.cancel()
						}
					})
				} else {
					return false
				}
			})
		},

		emailChange(obj, property) {
			obj[property] = obj[property].replace(/；/g, ';')
		},

		edit(obj) {
			this.form = JSON.parse(JSON.stringify(obj))
		}
	}
}
</script>
<style lang="scss" scoped ></style>
